/*
 Erica Sadun, http://ericasadun.com
 iPhone Developer's Cookbook, 3.0 Edition
 BSD License, Use at your own risk
 */

#import "UIViewExt.h"

CGPoint CGRectGetCenter(CGRect rect)
{
    CGPoint pt;
    pt.x = CGRectGetMidX(rect);
    pt.y = CGRectGetMidY(rect);
    return pt;
}

CGRect CGRectMoveToCenter(CGRect rect, CGPoint center)
{
    CGRect newrect = CGRectZero;
    newrect.origin.x = center.x-CGRectGetMidX(rect);
    newrect.origin.y = center.y-CGRectGetMidY(rect);
    newrect.size = rect.size;
    return newrect;
}

@implementation UIView (ViewGeometry)

// Retrieve and set the origin
- (CGPoint) origin
{
	return self.frame.origin;
}

- (void) setOrigin: (CGPoint) aPoint
{
	CGRect newframe = self.frame;
	newframe.origin = aPoint;
	self.frame = newframe;
}


// Retrieve and set the size
- (CGSize) size
{
	return self.frame.size;
}

- (void) setSize: (CGSize) aSize
{
	CGRect newframe = self.frame;
	newframe.size = aSize;
	self.frame = newframe;
}

// Query other frame locations
- (CGPoint) bottomRight
{
	CGFloat x = self.frame.origin.x + self.frame.size.width;
	CGFloat y = self.frame.origin.y + self.frame.size.height;
	return CGPointMake(x, y);
}

- (CGPoint) bottomLeft
{
	CGFloat x = self.frame.origin.x;
	CGFloat y = self.frame.origin.y + self.frame.size.height;
	return CGPointMake(x, y);
}

- (CGPoint) topRight
{
	CGFloat x = self.frame.origin.x + self.frame.size.width;
	CGFloat y = self.frame.origin.y;
	return CGPointMake(x, y);
}


// Retrieve and set height, width, top, bottom, left, right
- (CGFloat) height
{
	return self.frame.size.height;
}

- (void) setHeight: (CGFloat) newheight
{
	CGRect newframe = self.frame;
	newframe.size.height = newheight;
	self.frame = newframe;
}

- (CGFloat) width
{
	return self.frame.size.width;
}

- (void) setWidth: (CGFloat) newwidth
{
	CGRect newframe = self.frame;
	newframe.size.width = newwidth;
	self.frame = newframe;
}

- (CGFloat) top
{
	return self.frame.origin.y;
}

- (void) setTop: (CGFloat) newtop
{
	CGRect newframe = self.frame;
	newframe.origin.y = newtop;
	self.frame = newframe;
}

- (CGFloat) left
{
	return self.frame.origin.x;
}

- (void) setLeft: (CGFloat) newleft
{
	CGRect newframe = self.frame;
	newframe.origin.x = newleft;
	self.frame = newframe;
}

- (CGFloat) bottom
{
	return self.frame.origin.y + self.frame.size.height;
}

- (void) setBottom: (CGFloat) newbottom
{
	CGRect newframe = self.frame;
	newframe.origin.y = newbottom - self.frame.size.height;
	self.frame = newframe;
}

- (CGFloat) right
{
	return self.frame.origin.x + self.frame.size.width;
}

- (void) setRight: (CGFloat) newright
{
	CGFloat delta = newright - (self.frame.origin.x + self.frame.size.width);
	CGRect newframe = self.frame;
	newframe.origin.x += delta ;
	self.frame = newframe;
}

// Move via offset
- (void) moveBy: (CGPoint) delta
{
	CGPoint newcenter = self.center;
	newcenter.x += delta.x;
	newcenter.y += delta.y;
	self.center = newcenter;
}

// Scaling
- (void) scaleBy: (CGFloat) scaleFactor
{
	CGRect newframe = self.frame;
	newframe.size.width *= scaleFactor;
	newframe.size.height *= scaleFactor;
	self.frame = newframe;
}

// Ensure that both dimensions fit within the given size by scaling down
- (void) fitInSize: (CGSize) aSize
{
	CGFloat scale;
	CGRect newframe = self.frame;
	
	if (newframe.size.height && (newframe.size.height > aSize.height))
	{
		scale = aSize.height / newframe.size.height;
		newframe.size.width *= scale;
		newframe.size.height *= scale;
	}
	
	if (newframe.size.width && (newframe.size.width >= aSize.width))
	{
		scale = aSize.width / newframe.size.width;
		newframe.size.width *= scale;
		newframe.size.height *= scale;
	}
	
	self.frame = newframe;	
}



- (void)setXv:(CGFloat)xv
{
    CGRect frame = self.frame;
    frame.origin.x = xv;
    self.frame = frame;
}

- (CGFloat)xv
{
    return self.frame.origin.x;
}

- (void)setYv:(CGFloat)yv
{
    CGRect frame = self.frame;
    frame.origin.y = yv;
    self.frame = frame;
}

- (CGFloat)yv
{
    return self.frame.origin.y;
}

- (void)setCenterXv:(CGFloat)centerXv
{
    CGPoint center = self.center;
    center.x = centerXv;
    self.center = center;
}

- (CGFloat)centerXv
{
    return self.center.x;
}

- (void)setCenterYv:(CGFloat)centerYv
{
    CGPoint center = self.center;
    center.y = centerYv;
    self.center = center;
}

- (CGFloat)centerYv
{
    return self.center.y;
}

- (void)setWidthv:(CGFloat)widthv
{
    CGRect frame = self.frame;
    frame.size.width = widthv;
    self.frame = frame;
}

- (CGFloat)widthv
{
    return self.frame.size.width;
}

- (void)setHeightv:(CGFloat)heightv
{
    CGRect frame = self.frame;
    frame.size.height = heightv;
    self.frame = frame;
}

- (CGFloat)heightv
{
    return self.frame.size.height;
}

- (void)setSizev:(CGSize)sizev
{
    CGRect frame = self.frame;
    frame.size = sizev;
    self.frame = frame;
}

- (CGSize)sizev
{
    return self.frame.size;
}

- (CGPoint) originv
{
    return self.frame.origin;
}

- (void) setOriginv: (CGPoint) aPointv
{
    CGRect newframe = self.frame;
    newframe.origin = aPointv;
    self.frame = newframe;
}

- (CGFloat) topv
{
    return self.frame.origin.y;
}

- (void) setTopv: (CGFloat) newtopv
{
    CGRect newframe = self.frame;
    newframe.origin.y = newtopv;
    self.frame = newframe;
}

- (CGFloat) leftv
{
    return self.frame.origin.x;
}

- (void) setLeftv: (CGFloat) newleftv
{
    CGRect newframe = self.frame;
    newframe.origin.x = newleftv;
    self.frame = newframe;
}

- (CGFloat) bottomv
{
    return self.frame.origin.y + self.frame.size.height;
}

- (void) setBottomv: (CGFloat) newbottomv
{
    CGRect newframe = self.frame;
    newframe.origin.y = newbottomv - self.frame.size.height;
    self.frame = newframe;
}

- (CGFloat) rightv
{
    return self.frame.origin.x + self.frame.size.width;
}

- (void) setRightv: (CGFloat) newrightv
{
    CGFloat delta = newrightv - (self.frame.origin.x + self.frame.size.width);
    CGRect newframe = self.frame;
    newframe.origin.x += delta ;
    self.frame = newframe;
}

- (CGPoint) bottomRightv
{
    CGFloat x = self.frame.origin.x + self.frame.size.width;
    CGFloat y = self.frame.origin.y + self.frame.size.height;
    return CGPointMake(x, y);
}

- (CGPoint) bottomLeftv
{
    CGFloat x = self.frame.origin.x;
    CGFloat y = self.frame.origin.y + self.frame.size.height;
    return CGPointMake(x, y);
}

- (CGPoint) topRightv
{
    CGFloat x = self.frame.origin.x + self.frame.size.width;
    CGFloat y = self.frame.origin.y;
    return CGPointMake(x, y);
}

-(UIViewController *)viewController
{
    //下一个响应者
    UIResponder *next=[self nextResponder];
    do {
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next=[next nextResponder];
    } while (next!=nil);
    
    return nil;
}


//切圆
-(void)clipUIView:(CGFloat)r{
    self.layer.cornerRadius = r;
    self.layer.masksToBounds = YES;
}
//边框
-(void)setBorder:(UIColor*)color With:(CGFloat)width{
    self.layer.borderColor = color.CGColor;
    self.layer.borderWidth = width;
}
//图片裁剪
-(void)setImageMode{
    self.contentMode = UIViewContentModeScaleAspectFill;
    self.clipsToBounds = YES;
}

@end
